import { defineStore } from "pinia";
import { reqC1, reqC2, reqC3 } from "@/api/product/attr";
import type { CategoryResponseData } from "@/api/product/attr/type";
import type { CategoryState } from "./types/type";

let useCategoryStore = defineStore("category", {
  state: (): CategoryState => {
    return {
      c1Arr: [],
      c1Id: "",
      c2Arr: [],
      c2Id: "",
      c3Arr: [],
      c3Id: "",
    };
  },
  actions: {
    async getC1() {
      const result: CategoryResponseData = await reqC1();
      if (result.code === 200) {
        this.c1Arr = result.data;
      }
    },
    getC2() {
      reqC2(this.c1Id).then((result: CategoryResponseData) => {
        if (result.code === 200) {
          this.c2Arr = result.data;
        }
      });
    },
    getC3() {
      reqC3(this.c2Id).then((result: CategoryResponseData) => {
        if (result.code === 200) {
          this.c3Arr = result.data;
        }
      });
    },
  },
});

export default useCategoryStore;
